package preload 
{
	import flash.display.BitmapData;
	import flash.utils.ByteArray;
	import morn.core.handlers.Handler;
	import morn.core.managers.ResLoader;
	import org.airmvc.Controller;
	import org.airmvc.ModulesManager;
	import utils.T;
	
	/**
	 * 预加载控制器
	 * @author WLDragon 2014-02-24
	 */
	public class PreLoadC extends Controller 
	{
		
		public function PreLoadC() 
		{
			super();
		}
		
		override protected function addListeners():void 
		{
			
		}
		
		override protected function startup(... args):void 
		{
			//加载进度条资源
			App.loader.loadAssets(["assets/ui/preload.swf"],new Handler(onComplete));
			
		}
		
		private function onComplete():void 
		{
			//初始化预加载界面
			view = new PreLoadV();
			registerView(view);
			send(SHOW_OR_HIDE);
			
			//progressMsgs = [
			//"加载公共资源",
			//"加载开始模块资源",
			//"加载武将信息资源",
			//"加载菜单资源",
			//"加载战略模块资源",
			//"加载信息模块资源",
			//"加载战场模块资源",
			//"加载事件资源",
			//"加载武将头像资源",
			//"加载战斗模块资源"
			//];
			//preValue = -1;
			
			//加载其他模块的图片资源占90%
			var arr:Array = [
			{url:"assets/ui/common.swf", type:ResLoader.SWF, size:2 },
			{url:"assets/ui/begin.swf", type:ResLoader.SWF, size:504 },
			{url:"assets/ui/general.swf", type:ResLoader.SWF, size:2 },
			{url:"assets/ui/menu.swf", type:ResLoader.SWF, size:52 },
			{url:"assets/ui/strategy.swf", type:ResLoader.SWF, size:144 },
			{url:"assets/ui/infomation.swf", type:ResLoader.SWF, size:14 },
			{url:"assets/ui/march.swf", type:ResLoader.SWF, size:130 },
			{url:"assets/ui/report.swf", type:ResLoader.SWF, size:930 },
			{url:"assets/ui/heads.swf", type:ResLoader.SWF, size:497 },
			{url:"assets/ui/fight.swf", type:ResLoader.SWF, size:277 }
			];
			
			App.loader.loadAssets(arr,new Handler(onUiComplete),new Handler(onUiProgress));
		}
		
		private function onUiProgress(value:Number):void 
		{
			//var progMsg:String;
			//if (preValue != value)
			//{
				//progMsg = progressMsgs.shift();
				//trace(progMsg,value);
			//}
			send(UPDATE_PROGRESS, "加载UI资源", 0.9 * value);
		}
		
		private function onUiComplete():void 
		{
			//初始化地图纹理
			var bmd:BitmapData = App.asset.getBitmapData("png.march.map");
			T.texture.init(bmd, 32, 32);
			
			//加载游戏初始化数据并解析占10%
			App.loader.loadBYTE("assets/config/data.csv",new Handler(onParseData,[Cnt.DB_TYPE_VO]));
		}
		
		private function onParseData(type:int,bytes:ByteArray):void 
		{
			bytes.uncompress();
			switch(type)
			{
				case Cnt.DB_TYPE_VO:
					T.db.parseData(bytes, type);
					send(UPDATE_PROGRESS,"解析VO数据",0.925);
					App.loader.loadBYTE("assets/config/xml.csv",new Handler(onParseData,[Cnt.DB_TYPE_XML]));
				break;
				case Cnt.DB_TYPE_XML:
					T.db.parseData(bytes, type);
					send(UPDATE_PROGRESS,"解析XML数据",0.95);
					App.loader.loadBYTE("assets/config/maps.json",new Handler(onParseData,[Cnt.DB_TYPE_MAPB]));
				break;
				case Cnt.DB_TYPE_MAPB:
					T.db.parseData(bytes, type);
					send(UPDATE_PROGRESS,"解析地图B数据",0.975);
					App.loader.loadBYTE("assets/config/maps.csv",new Handler(onParseData,[Cnt.DB_TYPE_MAPD]));
				break;
				case Cnt.DB_TYPE_MAPD:
					T.db.parseData(bytes, type);
					send(UPDATE_PROGRESS, "解析地图D数据", 1);
					
					//销毁视图，打开开始模块
					send(DISPOSE);
					view = null;
					broadcast(Nws.STARTUP_CORE,Nws.STARTUP_CORE);
					broadcast(Nws.STARTUP_BEGIN, Nws.STARTUP_BEGIN);
					broadcast(Nws.STARTUP_GENERAL,Nws.STARTUP_GENERAL);
					ModulesManager.remove(Nws.STARTUP_PRELOAD);
				break;
			}
		}
		
		//private var progressMsgs:Array;
		//private var preValue:Number;
		//private var progMsg:String;

		/**打开或关闭视图*/
		static public const SHOW_OR_HIDE:String = "show_or_hide";
		/**通知进度*/
		static public const UPDATE_PROGRESS:String = "update_progress";
		/**释放资源*/
		static public const DISPOSE:String = "dispose";
	}
	
}